package com.leha.mp.core.services.imp;

import com.leha.mp.core.constants.CoreErrorDict;
import com.leha.mp.core.dao.CustomLogicMapper;
import com.leha.mp.core.dao.PlDoctorBillMapper;
import com.leha.mp.core.dao.PlDoctorWalletMapper;
import com.leha.mp.core.entity.mybatis.PlDoctorBill;
import com.leha.mp.core.entity.mybatis.PlDoctorBillExample;
import com.leha.mp.core.entity.mybatis.PlDoctorWallet;
import com.leha.mp.core.entity.vo.DoctorIncomeVo;
import com.leha.mp.core.services.BalanceServices;
import com.leha.mp.core.services.ErrorMessageServices;
import com.leha.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BalanceServicesImpl implements BalanceServices {
    @Autowired
    PlDoctorBillMapper billMapper;
    @Autowired
    PlDoctorWalletMapper walletMapper;
    @Autowired
    CustomLogicMapper customLogicMapper;
    @Autowired
    ErrorMessageServices errorMessageServices;

    @Override
    public DoctorIncomeVo GetDoctorIncome(Integer doctorId) {
        DoctorIncomeVo ret = new DoctorIncomeVo();
        ret.setTodayIncome(customLogicMapper.getDoctorTodayIncome(doctorId));
        PlDoctorWallet wa = walletMapper.selectByPrimaryKey(doctorId);
        if(wa == null) {
            ret.setWallet(UpdateDoctorWallet(doctorId));
        } else {
            ret.setWallet(wa);
        }
        return ret;
    }

    @Override
    public List<PlDoctorBill> GetDoctorBillList(Integer doctorId, String beginData, String endData) {
        PlDoctorBillExample ex = new PlDoctorBillExample();
        ex.createCriteria().andDoctorIdEqualTo(doctorId)
                .andCreateTimeGreaterThanOrEqualTo(DateTool.strToDate(beginData))
                .andCreateTimeLessThan(DateTool.strToDate(endData));
        return billMapper.selectByExample(ex);
    }

    @Override
    public Map<String, Double> GetDoctorIncomdeStatics(Integer doctorId, String beginData, String endData) {
        List<PlDoctorBill> bills = GetDoctorBillList(doctorId, beginData, endData);
        HashMap<String, Double> ret = new HashMap<>();
        for(PlDoctorBill bill : bills) {
            Double val = ret.get(bill.getType());
            if(val!=null) {
                ret.put(bill.getType().toString(), val+bill.getIncome());
            } else {
                ret.put(bill.getType().toString(), bill.getIncome());
            }
        }
        return ret;
    }

    @Override
    public Boolean AddBill(PlDoctorBill bill) {
        if(billMapper.insertSelective(bill)>0) {
            PlDoctorWallet wallet = walletMapper.selectByPrimaryKey(bill.getDoctorId());
            if(bill.getActiveTime()==null || bill.getActiveTime().getTime()<=new Date().getTime()) { //无冻结时间收入
                wallet.setTotal(wallet.getTotal() + bill.getIncome());
                wallet.setActive(wallet.getActive() + bill.getIncome());
            } else {
                wallet.setTotal(wallet.getTotal() + bill.getIncome());
                wallet.setFreeze(wallet.getFreeze() + bill.getIncome());
            }
            return true;
        }
        return false;
    }

    @Override
    public PlDoctorWallet UpdateDoctorWallet(Integer doctorId) {
        PlDoctorBillExample ex = new PlDoctorBillExample();
        ex.createCriteria().andDoctorIdEqualTo(doctorId);
        List<PlDoctorBill> bills = billMapper.selectByExample(ex);
        Double freeze = .0;
        Double active = .0;
        for(PlDoctorBill bill : bills) {
            if(bill.getActiveTime()==null || bill.getActiveTime().getTime()<=new Date().getTime()) { //无冻结时间收入
                active += bill.getIncome();
            } else {
                freeze += bill.getIncome();
            }
        }
        PlDoctorWallet wallet = walletMapper.selectByPrimaryKey(doctorId);
        if(wallet==null) {
            wallet = new PlDoctorWallet();
            wallet.setDoctorId(doctorId);
            wallet.setTotal(freeze+active);
            wallet.setWithdraw(.0);
            walletMapper.insertSelective(wallet);
        } else {
            wallet.setFreeze(freeze);
            wallet.setActive(active);
            walletMapper.updateByPrimaryKeySelective(wallet);
        }
        return wallet;
    }

    @Override
    public Boolean WithDraw(Integer DoctorId, Double money) {
        PlDoctorWallet wallet = walletMapper.selectByPrimaryKey(DoctorId);
        if(money>wallet.getActive()) {
            errorMessageServices.setError(CoreErrorDict.NoEnoughMoney);
            return false;
        }
        wallet.setActive(wallet.getActive()-money);
        wallet.setWithdraw(wallet.getWithdraw()+money);
        walletMapper.updateByPrimaryKeySelective(wallet);
        return true;
    }
}
